package com.dmx.fw.util;

import java.util.List;

/**
 * 分页查询
 * @author 沙北平
 * @date 2009年6月3日9:55:30
 */
public class PageInfo {
	
    /**
     * 总页数
     */
    private int totalPage = 1;

    /**
     * 前一页
     */
    private int prePage = 1;

    /**
     * 下一页
     */
    private int nextPage = 1;

    /**
     * 总记录数
     */
    private int totalRec = 0;

    /**
     * 默认每页记录数
     */

    private final int defaultPageSize = 10;


    /**
     * 每页记录数
     */
    private int pageSize = defaultPageSize;

    /**
     * 当前页码
     */
    private int pageIndex = 1;

    /**
     * 全部页码，从1开始
     */
    private int[] pageNumbers;
    
    /**
     * 全部页码，从1开始
     */
    private int[] curPageNumbers;

    /**
     * 要显示的页码个数
     */
    private int showPageCount = 7;

    /**
     * 是否存在下一页
     */
    private boolean hasNextPage = false;

    /**
     * 是否存在上一页
     */
    private boolean hasPrevPage = false;
    
    
	/**
	 * 页号式导航, 起始页号
	 */
	private int start;
	
	/**
	 * 页号式导航, 结束页号
	 */
	private int end;

    /**
     * 结果集合
     */
    private List<?> list;

	/**
	 * 第一页 页号
	 */
	private int first = 1;
	
	/**
	 * 最后页 页号
	 */
	private int last;
    
    
    
    public PageInfo() {

	}

	public int getPageIndex() {
        if (pageIndex > this.getTotalPage())
            this.setPageIndex(this.getTotalPage());
        
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex > 0 ? pageIndex : 1;
    }

    public int getNextPage() {
        return nextPage;
    }

    public void setNextPage(int nextPage) {
        this.nextPage = nextPage > this.totalPage ? this.totalPage : nextPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize > 0 ? pageSize : this.defaultPageSize;
    }

    public int getPrePage() {
        return prePage;
    }

    public void setPrePage(int prePage) {
        this.prePage = prePage < 1 ? 1 : prePage;
    }

    public int getTotalPage() {
        return this.totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage > 0 ? totalPage : 1;
    }

    public int getTotalRec() {
        return totalRec;
    }

    public void setTotalRec(int totalRec) {
        this.totalRec = totalRec > -1 ? totalRec : 0;
        this.setTotalPage((totalRec % this.pageSize == 0) ? (totalRec / this.pageSize)
                : (totalRec / this.pageSize) + 1);
        //System.out.println("pageInfo:" + this.getTotalPage());
    }

    public int[] getPageNumbers() {
        return pageNumbers;
    }

    public void setPageNumbers(int[] pageNumbers) {
        this.pageNumbers = pageNumbers;
    }

    public List<?> getList() {
        return list;
    }

    public void setList(List<?> list) {
        this.list = list;
    }

    public int[] getShowPageNumbers() {
        if (totalPage <= showPageCount) {
            return pageNumbers;
        } else {
            int[] newPageNumbers = new int[showPageCount];
            int startPageNumber = 1;
            if (pageIndex > (int) (showPageCount + 1) / 2) {
                startPageNumber += (pageIndex - (int) (showPageCount + 1) / 2);
            }
            if (startPageNumber + showPageCount >= totalPage) {
                startPageNumber = totalPage - showPageCount+1;
            }
            for (int i = 0; i < showPageCount; i++) {
                newPageNumbers[i] = startPageNumber + i;
            }
            return newPageNumbers;
        }
    }

    public int[] getShowDescPageNumbers() {

        int[] ascPageNumbers = getShowPageNumbers();
        int[] newPageNumbers = new int[ascPageNumbers.length];
        for (int i = ascPageNumbers.length; i > 0; i--) {
            newPageNumbers[ascPageNumbers.length - i] = ascPageNumbers[i - 1];
        }
        return newPageNumbers;
    }


    public int getShowPageCount() {
        return showPageCount;
    }

    public void setShowPageCount(int showPageCount) {
        this.showPageCount = showPageCount;
    }

    public boolean isHasNextPage() {
        if (pageIndex < totalPage) {
            hasNextPage = true;
        }
        return hasNextPage;
    }

    public void setHasNextPage(boolean hasNextPage) {
        this.hasNextPage = hasNextPage;
    }

    public boolean isHasPrevPage() {
        if (pageIndex > 1) {
            hasPrevPage = true;
        }
        return hasPrevPage;
    }

    public void setHasPrevPage(boolean hasPrevPage) {
        this.hasPrevPage = hasPrevPage;
    }

   
 
    public PageInfo(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getIndexRec() {
        return (pageIndex - 1) * pageSize + 1;
    }
    

    public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public int getLast() {
		return last;
	}

	public void setLast(int last) {
		this.last = last;
	}

	public int[] getCurPageNumbers() {
		return curPageNumbers;
	}

	public void setCurPageNumbers(int[] curPageNumbers) {
		this.curPageNumbers = curPageNumbers;
	}

	@Override
    public String toString() {
        return "PageInfo{" +
                "totalPage=" + totalPage +
                ", prePage=" + prePage +
                ", nextPage=" + nextPage +
                ", totalRec=" + totalRec +
                ", defaultPageSize=" + defaultPageSize +
                ", pageSize=" + pageSize +
                ", pageIndex=" + pageIndex +
                ", showPageCount=" + showPageCount +
                ", hasNextPage=" + hasNextPage +
                ", hasPrevPage=" + hasPrevPage +
                '}';
    }
}







